package domain;

import java.util.ArrayList;
import java.util.List;

public class Schedule implements Comparable<Schedule> {
	
	private Turn turn;
	private Day day;
	private PTime startTime;
	private PTime duration;
	private List<Allocation> allocations;
	
	public Schedule(Day day, Turn turn, PTime startTime, PTime duration) {
		this.setDay(day);
		this.setTurn(turn);
		this.setStartTime(startTime);
		this.setDuration(duration);
		this.setAllocations(new ArrayList<Allocation>());
	}
	
	public Day getDay() {
		return this.day;
	}
	
	public void setDay(Day day) {
		this.day = day;
	}

	public Turn getTurn() {
		return turn;
	}
	
	public void setTurn(Turn turn) {
		this.turn = turn;
	}

	public PTime getStartTime() {
		return startTime;
	}
	
	public void setStartTime(PTime startTime) {
		this.startTime = startTime;
	}

	public PTime getDuration() {
		return duration;
	}
	
	public void setDuration(PTime duration) {
		this.duration = duration;
	}

	public List<Allocation> getAllocations() {
		return allocations;
	}
	
	public void setAllocations(List<Allocation> allocations) {
		this.allocations = allocations;
	}
	
	public void addAllocation(Allocation a) {
		this.allocations.add(a);
	}
	
	public void removeAllocation(Allocation a) {
		this.allocations.remove(a);
	}
	
	public int numberOfAllocations() {
		return this.getAllocations().size();
	}
	
	@Override
	public boolean equals(Object o) {
		if (o == null || this.getClass() != o.getClass()) {
			return false;
		}
		if (o == this) {
			return true;
		}
		Schedule s = (Schedule) o;
		return this.getDay().equals(s.getDay())
			&& this.getTurn().equals(s.getTurn()) 
			&& this.getStartTime().equals(s.getStartTime())
			&& this.getDuration().equals(s.getDuration());
	}
	
	@Override
	public int compareTo(Schedule other) {
		final int BEFORE = -1;
		final int AFTER = 1;
		
		int compareTurn = this.getTurn().compareTo(other.getTurn()); 
		if (compareTurn < 0) {
			return BEFORE;
		} else if (compareTurn == 0) {
			int compareDay = this.getDay().compareTo(other.getDay()); 
			if (compareDay < 0) {
				return BEFORE;
			} else if (compareDay == 0) {
				return this.getStartTime().compareTo(other.getStartTime());
			} else {
				return AFTER;
			}
		} else {
			return AFTER;
		}
	}
	
	@Override
	public String toString() {
		String sep = System.getProperty("line.separator");
		String s = this.getStartTime() + " - " + PTime.sum(getStartTime(), getDuration());
		for (Allocation p : this.getAllocations()) {
			s += sep + p;
		}
		return s;
	}

}
